Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Listing 18.6 Code in FTP_DEMO.FRM.
    Option Explicit

    Private Sub Command1_Click(Index As Integer)

    Select Case Index
        Case 0 ‘ Logon
            txtDirectory.Text = “”
            Call LogOnToHost
        Case 1 ‘ Refresh
            Call RefreshDir
        Case 2 ‘ LogOff
            txtDirectory.Text = “”
            Inet1.Cancel
            Call SetCmdButtons(False)
            lblStatus.Caption = “Logged Off”
        Case 3 ‘ End
            Inet1.Cancel
            End
    End Select

    End Sub

    Private Sub Command2_Click(Index As Integer)

    Select Case Index
        Case 0 ‘ put
            If txtLocalFileName.Text = “” Then
                MsgBox (“Enter a file name to put.”)
                Exit Sub
            End If
            Call PutFile
        Case 1 ‘ get
            If txtRemoteFileName.Text = “” Then
                MsgBox (“Enter a file name to get.”)
                Exit Sub
            End If
            Call GetFile
        Case 2 ‘ cd
            If txtRemoteDir.Text = “” Then
                MsgBox (“Enter a directory name.”)
                Exit Sub
            End If
            Call ChangeDir
    End Select

    End Sub

    Private Sub Form_Load()

    Call SetCmdButtons(False)

    End Sub

    Private Sub Inet1_StateChanged(ByVal State As Integer)

    Dim vtData As Variant
    Dim strData As String

    Select Case State
        Case icResolvingHost ‘ 1
            lblStatus.Caption = _
                “Looking up IP address of host computer”
        Case icHostResolved  ‘ 2
            lblStatus.Caption = _
                “IP address found”
        Case icConnecting ‘ 3
            lblStatus.Caption = _
                “Connecting to host computer”
        Case icConnected ‘ 4
            lblStatus.Caption = “Connected”
        Case icRequesting ‘ 5
            lblStatus.Caption = _
                “Sending a request to host computer”
        Case icRequestSent ‘ 6
            lblStatus.Caption = “Request sent”
        Case icReceivingResponse ‘ 7
            lblStatus.Caption = _
                “Receiving a response from host computer”
        Case icResponseReceived ‘ 8
            lblStatus.Caption = “Response received”
        Case icDisconnecting ‘ 9
            lblStatus.Caption = _
                “Disconnecting from host computer”
        Case icDisconnected ‘ 10
            lblStatus.Caption = “Disconnected”
        Case icError ‘ 11
            lblStatus.Caption = “Error ” _
                & Inet1.ResponseCode & _
                “ ” & Inet1.ResponseInfo
        Case icResponseCompleted ‘ 12
            lblStatus.Caption = _
                “Request completed - all data received”
            ‘ Get first chunk.
            Do While True
                vtData = Inet1.GetChunk(1024, icString)
                If Len(vtData) = 0 Then Exit Do
                DoEvents
                strData = strData & vtData
             Loop
             txtDirectory.Text = strData
    End Select

    End Sub

    Public Sub LogOnToHost()

    ‘ Logs on to the specified FTP host and
    ‘ displays the directory.

    On Error GoTo ErrorHandler

    If txtURL = “” Or txtPassword = “” Then
        MsgBox (“You must specify a URL and Password”)
        Exit Sub
    End If

    Command1(0).Enabled = False
    Inet1.Protocol = icFTP
    Inet1.URL = txtURL.Text
    If txtUserName.Text = “” Then
        Inet1.UserName = “anonymous”
    Else
        Inet1.UserName = txtUserName.Text
    End If
    Inet1.Password = txtPassword.Text
    Inet1.Execute , “DIR”
    Call SetCmdButtons(True)

    Exit Sub

    ErrorHandler:

    If Err = 35754 Then
        MsgBox (“Unable to connect to remote host”)
    Else
        MsgBox (Err.Description)
    End If
    Call SetCmdButtons(False)
    Inet1.Cancel

    End Sub

    Public Sub PutFile()

    ‘ Puts the local file specified in
    ‘ txtLocalFileName to the remote server.

    Dim RemoteFileName As String, cmd As String

    RemoteFileName = InputBox(“Name for remote file?”, _
        “Get”, txtLocalFileName.Text)

    cmd = “PUT ” & RemoteFileName & “ ” _
        & txtLocalFileName.Text
    If InetCtrlIsReady(True) Then
        Call SendCommand(cmd)
    End If

    Exit Sub

    End Sub

    Public Sub GetFile()

    ‘ Retrieves the file specified in txtRemoteFileName.
    ‘ Stores using user-specified local name.
    Dim LocalFileName As String, cmd As String

    On Error GoTo ErrorHandler

    LocalFileName = InputBox(“Name for local file?”, _
        “Get”, txtRemoteFileName.Text)

    cmd = “GET ” & txtRemoteFileName.Text & “ ” & _
        LocalFileName
    If InetCtrlIsReady(True) Then
        Call SendCommand(cmd)
    End If

    Exit Sub

    ErrorHandler:
    MsgBox (Err.Description)

    End Sub

    Public Sub ChangeDir()

    Dim cmd As String

    ‘ Changes to the remote directory specified
    ‘ in txtRemoteDir.Text then displays the
    ‘ directory.

    cmd = “CD ” & txtRemoteDir.Text
    If InetCtrlIsReady(True) Then
        Call SendCommand(cmd)
        Do
            DoEvents
        Loop Until InetCtrlIsReady(False)
        txtDirectory.Text = “”
        Call SendCommand(“DIR”)
    End If

    End Sub

    Public Sub SetCmdButtons(LoggedOn As Boolean)

    Dim i As Integer

    ‘ Sets command button states for logged on
    ‘ or logged off situation.

    If LoggedOn Then
    ‘ Change command buttons for logged on state.
        Command1(0).Enabled = False ‘ Logon button
        Command1(1).Enabled = True ‘ Refresh button
        Command1(2).Enabled = True ‘ Logoff button
        For i = 0 To Command2.Count - 1
            Command2(i).Enabled = True
        Next
    Else
    ‘ Change command buttons for logged off state.
        Command1(0).Enabled = True  ‘ Logon
        Command1(1).Enabled = False ‘ Refresh button
        Command1(2).Enabled = False ‘ Logoff button
        For i = 0 To Command2.Count - 1
            Command2(i).Enabled = False
        Next
    End If

    End Sub

    Public Sub RefreshDir()

    ‘ Refreshes the remote directory listing.

    If InetCtrlIsReady(True) Then
        txtDirectory.Text = “”
        Call SendCommand(“DIR”)
    End If

    End Sub

    Public Sub SendCommand(cmd As String)

    On Error GoTo ErrorHandler

    Inet1.Execute , cmd
    Exit Sub

    ErrorHandler:

    MsgBox (Err.Description)
    Resume Next

    End Sub

    Public Function InetCtrlIsReady(ShowMessage As Boolean) _
        As Boolean

    ‘ Returns True if the Internet Transfer Control is
    ‘ ready to execute another command. Displays an
    ‘ error message if the control is busy and ShowMessage
    ‘ is True.

    Dim buf As String

    If Inet1.StillExecuting Then
        If ShowMessage Then
            buf = “The control has not ”
            buf = buf & “completed executing” & vbCrLf
            buf = buf & “the last request. ”
            buf = buf & “Please wait and try again.”
            MsgBox (buf)
        End If
        InetCtrlIsReady = False
    Else
        InetCtrlIsReady = True
    End If

    End Function
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.